package com.datastructure.test.maxmultiofcontinuouschildrenarray;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;

public class MaxMultiOfContinuousChildArray {

    public static void main(String[] args) {
        MaxMultiOfContinuousChildArray maxMultiOfContinuousChildArray = new MaxMultiOfContinuousChildArray();
        int[] data = {3,2,-1,4};
        maxMultiOfContinuousChildArray.maxProduct(data);
    }

    public int maxProduct (int[] nums) {
        // write code here
        // 动态规划
        int previousMaximum = nums[0];
        int previousMinimum = nums[0];
        int currentMaximum = nums[0];
        int currentMinimum = nums[0];
        int rs = nums[0];
        for (int i = 2; i <= nums.length; i++) {
            currentMaximum = Math.max(Math.max(nums[i-1], previousMaximum*nums[i-1]), previousMinimum*nums[i-1]);
            currentMinimum = Math.min(Math.min(nums[i-1], previousMaximum*nums[i-1]), previousMinimum*nums[i-1]);
            rs = Math.max(rs, currentMaximum);
            previousMaximum = currentMaximum;
            previousMinimum = currentMinimum;
        }
        return rs;
    }
}
